import cy_config
import cy_operate
import cy_log
import open_more
import open_less
from multiprocessing import Pool

def makeOpenMore(base_section_prices, pos):

    cy_log.printAndInLog('创建一个开多的区间')

    # 创建开多对象
    openMore_item = open_more.OpenMore(base_section_prices, 1)

    pos.apply_async(openMore_item.open)

def makeOpenLess(base_section_prices, pos):

    cy_log.printAndInLog('创建一个开空的区间')

    # 创建开空对象
    openLess_item = open_less.OpenLess(base_section_prices, 2)

    pos.apply_async(openLess_item.open)

# 初始化配置
def initDefaultConfig():
    
    # 修改用户的开仓杠杆
    cy_operate.changeAllSymbolLeverage()

    # 获取账号开仓时的总金额
    cy_config.open_total_price = cy_operate.getBlan()

    # 保存金额到本地
    cy_operate.saveOpenPriceToLocal()

if __name__ == '__main__':

    initDefaultConfig()

    # 根据当前币种最新价格 得到当前价格区间
    base_price = cy_operate.getCurrentPrice()

    if base_price:

        # 得到开多区间
        more_base_price = float(base_price) * 0.998
        more_base_section_prices = cy_operate.getMorePrices(more_base_price)

        # 保存多区间到本地
        cy_operate.saveMoreQJDataToLocal(more_base_section_prices)

        # 得到开空区间
        less_base_price = float(base_price) * 1.002
        less_base_section_prices = cy_operate.getLessPrices(less_base_price)

        # 保存空区间到本地
        cy_operate.saveLessQJDataToLocal(less_base_section_prices)

        pos = Pool(2)

        if cy_config.open_type == 1:
            makeOpenMore(more_base_section_prices, pos)
        elif cy_config.open_type == 2:
            makeOpenLess(less_base_section_prices, pos)
        elif cy_config.open_type == 3:
            makeOpenMore(more_base_section_prices, pos)
            makeOpenLess(less_base_section_prices, pos)

        pos.close()
        pos.join()

    else:
        cy_log.printAndInLog('价格获取失败 区间初始化失败')

    cy_log.printAndInLog('主线结尾')